<?php
/**
 * @author: xaboy<365615158@qq.com>
 * @day: 2017/11/11
 */

namespace app\admin\model\user;

use app\admin\model\system\SystemUserLevel;
use traits\ModelTrait;
use basic\BaseModel;
use app\admin\model\wechat\WechatUser;
use app\admin\model\store\StoreCouponUser;

/**
 * 用户管理 model
 * Class User
 * @package app\admin\model\user
 */
class User extends BaseModel
{

    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';

    /**
     * 模型名称
     * @var string
     */
    protected $name = 'user';

    use ModelTrait;



    /**
     * @param $where
     * @return array
     */
    public static function systemPage($where)
    {
        $model = new self;
        if ($where['status'] != '') $model = $model->where('status', $where['status']);
        if (isset($where['user_type']) && $where['user_type'] != '') $model = $model->where('user_type', $where['user_type']);
        if ($where['nickname'] != '') $model = $model->where('nickname|uid', 'like', "%$where[nickname]%");
        $model = $model->order('uid desc');
        return self::page($model, function ($item) {
            if ($item['spread_uid']) {
                $item['spread_uid_nickname'] = self::where('uid', $item['spread_uid'])->value('nickname');
            } else {
                $item['spread_uid_nickname'] = '无';
            }
        }, $where);
    }


                /**
     * 获取查询条件
     * @param $where
     * @param string $alert
     * @param null $model
     * @return SystemUserLevel|null
     */
    public static function setWhere($where,$alert='',$model=null)
    {
        $model=$model===null ? new self() : $model;
        if(isset($where['nickname']) && $where['nickname']) $model=$model->where("name",'like',"%{$where['name']}%");
        return $model;
    }

    /**
     * 异步获取当前用户 信息
     * @param $where
     * @return array
     */
    public static function getUserList($where)
    {
        $data=self::setWhere($where)->page((int)$where['page'],(int)$where['limit'])->field('id,openid,nickname,name,mobile,avatar_url,credit_card')->select();
        $data=count($data) ? $data->toArray() : [];
        $count=self::setWhere($where)->count();
        return compact('data','count');
    }

    /**
     *  修改用户状态
     * @param $uids 用户uid
     * @param $status 修改状态
     * @return array
     */
    public static function destrSyatus($uids, $status)
    {
        if (empty($uids) && !is_array($uids)) return false;
        if ($status == '') return false;
        self::beginTrans();
        try {
            $res = self::where('uid', 'in', $uids)->update(['status' => $status]);
            self::checkTrans($res);
            return true;
        } catch (\Exception $e) {
            self::rollbackTrans();
            return Json::fail($e->getMessage());
        }
    }

    /*
     *  获取某季度,某年某年后的时间戳
     *
     * self::getMonth('n',1) 获取当前季度的上个季度的时间戳
     * self::getMonth('n') 获取当前季度的时间戳
     */
    public static function getMonth($time = '', $ceil = 0)
    {
        if (empty($time)) {
            $firstday = date("Y-m-01", time());
            $lastday = date("Y-m-d", strtotime("$firstday +1 month -1 day"));
        } else if ($time == 'n') {
            if ($ceil != 0)
                $season = ceil(date('n') / 3) - $ceil;
            else
                $season = ceil(date('n') / 3);
            $firstday = date('Y-m-01', mktime(0, 0, 0, ($season - 1) * 3 + 1, 1, date('Y')));
            $lastday = date('Y-m-t', mktime(0, 0, 0, $season * 3, 1, date('Y')));
        } else if ($time == 'y') {
            $firstday = date('Y-01-01');
            $lastday = date('Y-12-31');
        } else if ($time == 'h') {
            $firstday = date('Y-m-d', strtotime('this week +' . $ceil . ' day')) . ' 00:00:00';
            $lastday = date('Y-m-d', strtotime('this week +' . ($ceil + 1) . ' day')) . ' 23:59:59';
        }
        return array($firstday, $lastday);
    }

    public static function getcount()
    {
        return self::count();
    }


    /**获取用户详细信息
     * @param $uid
     * @return array
     */
    public static function getUserInfos($uid)
    {
        $userInfo = self::where('uid', $uid)->find();
        if (!$userInfo) exception('读取用户信息失败!');
        return $userInfo->toArray();
    }


}